package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.Map;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    //Integer page）, Integer rows, String supplierName
    @PostMapping("list")
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        return supplierService.list(page, rows, supplierName);
    }

    /**
     * 请求URL：
     * http://localhost:8080/supplier/save?supplierId=1
     * 请求参数：Supplier supplier
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：ServiceVO
     */
    @PostMapping("save")
    public ServiceVO save(Supplier supplier) {
        if (supplier != null) {
            if (null != supplier.getSupplierId() && !"".equals(String.valueOf(supplier.getSupplierId()))) {
                supplierService.update(supplier);
            } else {
                supplierService.insert(supplier);
            }
            return new ServiceVO(100, "请求成功!");
        }

        return new ServiceVO(200, "请求失败!");
    }



    /**
     * 请求URL：http://localhost:8080/supplier/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("delete")
    public ServiceVO delete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (null != ids && !"".equals(ids)){
            supplierService.delete(ids);
            return new ServiceVO(100, "请求成功!");

        }
        return new ServiceVO(200, "请求失败!");
    }
}
